package com.hxzy.service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;

import com.hxzy.dao.UserDao;
import com.hxzy.entity.User;
import com.hxzy.utils.Hashs;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public ServiceResult<User> register(String email, long phone, String username, String passwd, String nickname, String realname,
            String headImg) {
        // TODO Auto-generated method stub
        // HttpServletRequest request=ServletActionContext.getRequest();
        Map<String, Object> param=new HashMap<String, Object>();
        param.put("email", email);
        List<User> userList=userDao.read("from User where email =:email", param);
        if(!userList.isEmpty()){
            return new ServiceResult<User>(-1);//email is exists
        }
        Map<String, Object> param2 = new HashMap<String, Object>();
        param2.put("username", username);
        List<User> userList2 = userDao.read("from User where username =:username", param2);
        if (!userList2.isEmpty()) {
            return new ServiceResult<User>(-2);//username is exists
        }
        
        String salt = Hashs.createSalt();
        Calendar c = Calendar.getInstance();
        long now = (int) (c.getTimeInMillis() / 1000);
        User user = new User();
        user.setUsername(username);
        user.setPasswd(DigestUtils.sha1Hex((salt + passwd).getBytes()));
        user.setNickname(nickname);
        user.setRealname(realname);
        user.setSalt(salt);
        user.setEmail(email);
        user.setGmtCreate(now);
        user.setGmtModified(now);
        user.setIpReg("172.16.1.55");
        user.setStatus(0l);
        user.setHeadImg(headImg);
        user.setPhone(phone);
        userDao.create(user);
        return new ServiceResult<User>(1);
    }

    public User emailExists(String email) {
        // TODO Auto-generated method stub
        User user = null;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("email", email);
        List<User> userList = userDao.read("from User where email =:email", param);
        if (userList.isEmpty()) {
            return user;
        }
        user = userList.get(0);
        return user;
    }

    public boolean nameExists(String username) {
        // TODO Auto-generated method stub
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("username", username);
        List<User> userList = userDao.read("from User where username =:username", param);
        if (userList.isEmpty()) {
            return false;
        }
        return true;
    }

    public User login(String name, String passwd, String ip, String remeberMe) {
        // TODO Auto-generated method stub
        // User user=userDao.get(name, passwd);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("username", name);
        List<User> userList = userDao.read("from User where username =:username", param);
        User user = userList.get(0);
        if (user != null && user.getPasswd().equals(DigestUtils.sha1Hex((user.getSalt() + passwd).getBytes()))) {
            Calendar c = Calendar.getInstance();
            long now = (int) (c.getTimeInMillis() / 1000);
            int timeout = 2 * 60;
            user.setUsername(name);
            user.setGmtLogin(now);
            user.setGmtModified(now);
            user.setIpLogin(ip);
            user.setStatus(0l);
            // HttpServletResponse response=ServletActionContext.getResponse();
            if ("1".equals(remeberMe)) {
                String hash = DigestUtils.sha1Hex(Hashs.createSalt() + ip);
                Cookie cookie = new Cookie("h", hash);
                cookie.setMaxAge(timeout); // 盐值存储一天
                // response.addCookie(cookie);
                user.setHash(hash);
                user.setGmtHashExpires((now + timeout));
            } else {
                Cookie cookie = new Cookie("h", null);
                cookie.setMaxAge(0);
                // response.addCookie(cookie);
                user.setHash("");
                user.setGmtHashExpires(0l);
            }
            userDao.update(user);
            return user;
        }
        return user;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}
